import {
    getVideoList,
    addVideo,
    getTypeList,
    sortType,
    deleteVideo,
    moveToOther,
    loginUser,
    classList,
    videoInfo,
    
} from '../services/fetch'
import { informationList,addInformation,usedModule,deleteInformation,top,informationThemeInfo, cancelTop,auditVideo} from '../services/theme'

import { routerRedux } from 'dva/router';
import {
    message
} from 'antd';
import moment from 'moment';
export default {

    namespace: 'theme',

    state: {
        isShowModal: false,
        submitting: false,
        initTableName: '',
        treeList: [],
        dataSource: {},
        index: 1,
        selectedRowKeys: [],
        selectedPlateIds: [],//已选中的稿件id集合
        selectedPlateDatas: [],//已选中稿件data集合
        quanxian: [],
        selectedKeys: [],//已选中的树节点
        classList: [],
        dataAugment: {}, //查询数据
        params:{},
    },

    subscriptions: {
        setup({ dispatch, history }) {  // eslint-disable-line
        },
    },

    effects: {
        *getTreeList({ payload, callback }, { call, put, select }) {
            const list = yield call(getTypeList);
            const data = []
            list && list.data && list.data.data && list.data.data.map((item, index) => {
                let obj = {
                    key: String(index),
                    plate_id: item.plate_id,
                    title: item.plate_name,
                    children: item.children ? item.children : '',
                    value: String(index),
                    disabled: item.children ? true : false
                }
                if (item.children) {
                    item.children.map((i, ins) => {
                        let obj2 = {
                            key: String(index) + '-' + ins,
                            plate_id: i.plate_id,
                            title: i.plate_name,
                            value: String(index) + '-' + ins,
                        }
                        item.children[ins] = obj2
                    })
                }
                data[index] = obj
            })
            yield put({ type: 'updateTreeList', data: data });
            yield put({ type: 'updateTableName', payload: { initTableName: list.data.data[0].plate_id } });
            if (callback) callback(list.data.data);
        },
        /**
         * 提交审核
         * @param {} param0
         * @param {*} param1
         */
            *getAuditVideo({ data }, { call, put }) {
            const list = yield call(auditVideo,data);
            if(list.data.code === 200){
                localStorage.setItem('themeSubmit', Date.parse(new Date()));
                message.success('提交审核成功')
            }else{
                message.error('提交审核失败')
            }
        },
        /**
        * 新增咨询
        */
        *addInformation({ data,callback }, { call, put, select }) {
            if (data.crond_time){
                data.crond_time = moment(data.crond_time).format('YYYY-MM-DD HH:mm:ss');
            }
            if (data.lower_crond_time){
                data.lower_crond_time = moment(data.lower_crond_time).format('YYYY-MM-DD HH:mm:ss');
            }
            const res = yield call(addInformation, data);
            if (res.data.code === 200) {
                //
                // message.success(res.data.msg)
                localStorage.setItem('localSelectedThemeId', data.theme_id);
                localStorage.setItem('themeSubmit', Date.parse(new Date()));
            } else {
                message.error(res.data);

            }
            if(callback) {
                yield call(callback,res.data)
            }
        },
        /**
         * 获取资讯详情
         */
        *getInformationInfo({ data }, { call, put, select }) {
            yield put({ type: 'updateInformationInfo', data: data.data });
        },
        /**
         * 获取引用资讯详情
         */
        *getUseInformationInfo({ data }, { call, put, select }) {
            const res = yield call(videoInfo, data);
            if (res.data.code === 200) {
                yield put({ type: 'updateInformationInfo', data: res.data.data });
            }else{
                yield put({ type: 'updateInformationInfo', data: {} });
                message.error(res.data.msg)
            }
        },
        /**
         * 引用
         */
        *usedModule({ payload }, { call, put, select }) {
            const list = yield call(usedModule, payload.data);
            const videoList = yield call(informationList, payload.dataAugment);
            yield put({ type: 'updateTableList', data: videoList.data.data });
            message.success(list.data.msg)
        },
        /**
         * 复制
         */
        *copy({ payload }, { call, put, select }) {
            const list = yield call(addInformation, payload.data);
            const videoList = yield call(informationList, payload.dataAugment);
            yield put({ type: 'updateTableList', data: videoList.data.data });
            message.success(list.data.msg)
        },
        *getTableList({ payload }, { call, put, select }) {

            const list = yield call(informationList, payload.dataAugment);
            yield put({ type: 'dataAugment', payload: payload.dataAugment })
            yield put({ type: 'updateTableList', data: list.data.data });
        },
        /**
         * 删除
         * @param {} param0
         * @param {*} param1
         */
        *deleteTableList({ payload }, { call, put, select }) {
            const res = yield call(deleteInformation, payload.dataAugment);
            if(res.data.code == 200){
                message.success('删除成功');
            }
            const dataAugment = yield select(state => state.theme.dataAugment);
            yield put({ type: 'getTableList', payload: { dataAugment:dataAugment} })
            yield put({ type: 'updateMoreDeal', data: [] });
        },
        *backTreeList({ payload }, { call, put, select }) {
            yield call(sortType, payload.dataAugment);
            yield put({ type: 'updateTreeList', data: payload.dataAugment });
        },
        *addVideo({ data, callback }, { call, put, select }) {
            let a = yield call(addVideo, data);
            const initTableName = yield select(state => state.theme.initTableName);
            const list = yield call(getVideoList, { index: 1, plate_id: initTableName });
            yield put({ type: 'updateTableList', data: list.data.data });
            //yield put({ type: 'updateTableList', data: list });
        },
        *moveTableList({ payload }, { call, put, select }) {
            const res = yield call(moveToOther, payload.dataAugment);
            yield put({ type: 'updateMoreDeal', data: [] });
            if (res.code === 200) {
                yield put({ type: 'updateTreeList', data: payload.dataSource });
            }
        },
        *getQuanxian({ payload }, { call, put, select }) {
            let str = yield call(loginUser, payload.data);
            let arr = []
            if (str.data.code === 200) {
                arr = str.data.data.privilege_list
                yield put({ type: 'updateQuanxian', data: arr });
            } else if (str.data.code === 500) {
                message.error(str.data.msg);
                yield put(routerRedux.push('/note'));
            }
        },
        /**
         * 获取分类列表
         */
        *classList({ payload }, { call, put }) {
            let list = yield call(classList);
            if (list.data.code === 200) {
                yield put({ type: 'saveClassList', data: list.data.data })
            }
        },
        /**
         * 资讯置顶
         */
        *top({ data }, { call, put, select }) {
            let list = yield call(top, data);
            if (list.data.code === 200) {
                message.success('置顶成功')
                const initTableName = yield select(state => state.theme.initTableName);
                const list = yield call(informationList, { index: 1, theme_id: initTableName });
                yield put({ type: 'updateTableList', data: list.data.data });
            } else {
                message.error(list.data.msg)
            }
        },
        /**
         * 资讯取消置顶
         */
        *cancelTop({ data }, { call, put, select }) {
            let list = yield call(cancelTop, data);
            if (list.data.code === 200) {
                message.success('取消成功')
                const initTableName = yield select(state => state.theme.initTableName);
                const list = yield call(informationList, { index: 1, theme_id: initTableName });
                yield put({ type: 'updateTableList', data: list.data.data });
            } else {
                message.error(list.data.msg)
            }
        },
    },

    reducers: {
        updateInformationInfo(state, action) {
            return { ...state, informationInfo: action.data };
        },
        dataAugment(state, action) {
            return { ...state, dataAugment: action.payload };
        },
        saveClassList(state, action) {
            return { ...state, classList: action.data };
        },
        setSelectedPlateIds(state, action) {
            return { ...state, selectedPlateIds: action.data.ids, selectedPlateDatas: action.data.list };
        },
        showModal(state, action) {
            return { ...state, isShowModal: true };
        },
        hideModal(state, action) {
            return { ...state, isShowModal: false };
        },
        updateTableName(state, action) {
            return { ...state, initTableName: action.payload.initTableName, selectedKeys: action.payload.selectedKeys };
        },
        updateTreeList(state, action) {
            return { ...state, treeList: action.data };
        },
        updateTableList(state, action) {
            return { ...state, dataSource: action.data };
        },
        updateMoreDeal(state, action) {
            return { ...state, selectedRowKeys: action.data };
        },
        updateIndex(state, action) {
            return { ...state, index: action.data };
        },
        updateQuanxian(state, action) {
            return { ...state, quanxian: action.data };
        },
        setParams(state,action){
            return { ...state, params: action.data ,initTableName:action.data.id};
        }

    },

};
